<template>
  <div class="home">
    <!-- 轮播图 -->
    <div class="homesearch">
      <van-search
        v-model="value"
        background="rbga(0,0,0,0.0)"
        placeholder="搜索食材、商品"
        show-action
        action-text="搜索"
        @search="souso"
      >
        <template #action>
          <div @click="souso">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="searchshow" v-if="searchshow">
      <van-swipe
        class="my-swipe"
        :autoplay="3000"
        indicator-color="white"
        :style="{ height: h }"
        :show-indicators="false"
      >
        <van-swipe-item>
          <img src="../../assets/1.jpg" alt="" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../../assets/2.jpg" alt="" />
        </van-swipe-item>
        <van-swipe-item>
          <img src="../../assets/3.jpg" alt="" />
        </van-swipe-item>
      </van-swipe>
      <!-- 首页分类 -->
      <van-grid square icon-size="55px">
        <van-grid-item
          @click="detailsed(item.tid)"
          v-for="(item, i) of homecategory"
          :key="i"
          :icon="`/img/category/${item.typeavatar}`"
          :text="item.category"
        />
      </van-grid>
      <div class="titleItem">
        - 热门菜谱 -
        <!-- <span class="icon more smallFonts"></span> -->
      </div>
      <!-- 加载更多 -->
      <div>
        <!-- 列表数据 -->
        <div class="container"></div>
        <div class="item" v-for="(item, i) of 10" :key="i">
          <menurow :cid="i + 2" />
        </div>
      </div>
    </div>
    <div v-else class="menurow">
        <div class="issearch" v-if="issearch">对不起，暂时没有查询到...</div>
        <menurow v-for="(item, i) of searchlist" :key="i" :cid="item.cid" />
    </div>
    <tabbar></tabbar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      h: "250px",
      show: false,
      value: "",
      homecategory: [],
      searchshow: true,
      searchlist: [],
      issearch:false
    };
  },
  watch:{
    value(){
      if(this.value==""){
        this.searchshow=true
      }
    }
  },
  methods: {
    detailsed(i) {
      this.$router.push(`/menu/${i}`);
    },
    souso() {
      console.log(this.value == "");
      if (this.value != "") {
        this.axios.get(`/menu/keyword?key=${this.value}`).then((result) => {
          this.searchshow = false;
          if (result.data.code == 200) {
            this.issearch=false
            this.searchlist = result.data.result;
          }else if(result.data.code == 201){
            this.issearch=true
            this.searchlist=[]
          }
        });
      }
    },
  },
  mounted() {
    let screenWidth = window.screen.width;
    let screenHeight = Math.floor((699 * screenWidth) / 1242) + "px";
    this.h = screenHeight + "px";
    if (sessionStorage.getItem("categoey") == null) {
      this.axios.get("/menu/category").then((result) => {
        result.data.results.forEach((item) => {
          if (this.homecategory.length < 8) {
            this.homecategory.push(item);
          }
          sessionStorage.setItem("categoey", JSON.stringify(this.homecategory));
        });
      });
    } else {
      this.homecategory = JSON.parse(sessionStorage.getItem("categoey"));
    }
  },
};
</script>
<style lang="scss">
.home {
  .van-icon__image {
    width: 71px;
  }
  .my-swipe .van-swipe-item {
    color: #fff;
    font-size: 20px;
    line-height: 150px;
    text-align: center;
    width: 100%;
  }
  .my-swipe img {
    width: 100%;
  }
  .search {
    position: fixed;
    top: 0px;
    left: 70px;
    background-color: rgba(0, 0, 0, 0);
    opacity: 0.5;
  }
  .titleItem {
    margin: 10px;
    text-align: center;
  }
  .item {
    margin: 8px;
    background-color: rgba(250, 250, 250, 0.8);
  }
  .searchshow {
    // position: fixed;
    margin-top:54px ;
  }
  .homesearch {
    position: fixed;
    z-index: 100;
    width: 100%;
    top: 0px;
  }
  .menurow{
    position: fixed;
    top: 54px;
    // right: 0;
    bottom: 50px;
    overflow-y: scroll;
  }
  .issearch{
    margin: 10px 10px 0 12px;
    // text-align: center;
    color: rgb(83, 78, 78);
  }
}
</style>